using System;
using System.Diagnostics;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace StructureMap.WcfIntegration.Tests.Behaviours
{
    public class UnitOfWorkErrorHandler : IErrorHandler
    {
        private readonly IContainer container;


        public UnitOfWorkErrorHandler(IContainer container)
        {
            this.container = container;
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var uow = container.GetInstance<IUnitOfWork>();

            Debug.WriteLine("Rolling back UoW");

            uow.Rollback();
        }

        public bool HandleError(Exception error)
        {
            return false;
        }
    }
}